<?php

namespace App\Models\Sys;

use App\Models\School\Teacher;
use Illuminate\Database\Eloquent\Model;
use App\Models\School\Classes;

class School extends Model
{

    protected $table = 'jxt_sys_school';

    public function classes(){
        return $this->hasMany(Classes::class,'school_code','id');
    }

    public function teachers()
    {
        return $this->belongsToMany(Teacher::Class, 'jxt_school_classes', 'school_code', 'class_id');
    }

    public static function getTeacher($classes)
    {
        $teacher_count = 0;
        if ($classes) {
            $classes = $classes->toArray();
            $class_ids = array_column($classes, 'class_id');

            $teacher_count = Teacher::whereIn('class_id', $class_ids)->distinct('teacher_id')->count('teacher_id');
        }

        return $teacher_count;
    }

    public static function getParent($classes)
    {
        $teacher_count = 0;
        if ($classes) {
            $classes = $classes->toArray();
            $class_ids = array_column($classes, 'class_id');

            $teacher_count = Teacher::whereIn('class_id', $class_ids)->distinct('teacher_id')->count('teacher_id');
        }

        return $teacher_count;
    }

    /*public function getLocationAttribute($value)
    {
        $url = 'https://restapi.amap.com/v3/geocode/regeo?key=70edb8064b04e1cccaf63dccc34be4d6&location=';
        $location = get_url($url . $value);
        if ($location && $location['status'] == '1'  && $location['infocode'] == '10000') {
            return $location['regeocode']['formatted_address'];
        }

        return $value;
    }*/

    public static function getTeacherCount($id)
    {
        return Teacher::from('jxt_school_teacher AS a')
            ->leftJoin('jxt_school_classes as b', function ($join) use ($id) {
                $join->on('b.class_id', 'a.class_id')->where('b.school_code', $id);
            })
            ->leftJoin('jxt_user_account as c', 'c.user_id', 'a.user_id')
            ->leftJoin('jxt_xp_user as d', 'd.id', 'a.user_id')
            ->where('b.school_code', $id)
            ->where('a.status', 1)
            ->where('d.status', 1)
            ->count();

    }

    public static function getParentCount($id)
    {
        return Classes::from('jxt_school_classes as a')
            ->leftJoin('jxt_school_student as b', function ($join) {
                $join->on('b.class_id', 'a.class_id')->where('b.status', 1);
            })
            ->leftJoin('jxt_school_child_student as c', function ($join) {
                $join->on('c.student_id', 'b.student_id')->where('c.status', 1);
            })
            ->leftJoin('jxt_sys_child as d', function ($join) {
                $join->on('d.child_id', 'c.child_id')->where('d.status', 1);
            })
            ->rightJoin('jxt_xp_user as e', function ($join) {
                $join->on('e.id', 'd.create_by')->where('e.status', 1);
            })
            ->where('a.status', 1)
            ->where('a.school_code', $id)
            ->where('e.id', '>', 0)
            ->count();
    }


}
